home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’95
/
Greg's Hack
/
Antialias.c
next >
Wrap
Text File
|
1995-06-24
|
5KB
|
205 lines
/*
Greg's Hack — MacHack '95 Best Hack Contest (22-24 June 1995 )
Copyright © 1995 Gregory D. Landweber, ALL RIGHTS RESERVED
*/
#include <QDOffscreen.h>
#include <A4Stuff.h>
#include "AntiAlias.h"
void ShrinkMap ( PixMapHandle srceMap, PixMapHandle destMap, short width, short height);
GWorldPtr smallWorld, largeWorld;
long table[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
long table2[256];
Rect smallRect = { 0, 0, 32, 1024 },
largeRect = { 0, 0, 128, 4096 };
pascal void MyDrawChar ( char theChar )
{
MyDrawText ( &theChar, 0, 1 );
}
pascal void MyDrawString ( StringPtr theString )
{
MyDrawText ( (Ptr)theString, 1, theString[0] );
}
pascal void MyDrawText ( Ptr buffer, short offset, short len )
{
GWorldPtr saveGW;
GDHandle saveGD;
short txSize, txFont, txMode;
FontInfo fInfo;
Rect myRect, maskRect;
Point penPt;
short width;
short pixelSize;
PixMapHandle largePixMap;
Boolean onScreen;
Boolean colorPort;
Ptr BaseAddr;
EnterCodeResource();
GetGWorld ( &saveGW, &saveGD );
pixelSize = (*(*saveGD)->gdPMap)->pixelSize;
txFont = saveGW->txFont;
txSize = 4 * ( saveGW->txSize ? saveGW->txSize : ( LMGetSysFontSize() ? LMGetSysFontSize() : 12 ) );
txMode = saveGW->txMode;
colorPort = ( (saveGW->portVersion & 0xC000) == 0xC000 );
BaseAddr = ((GrafPtr)saveGW)->portBits.baseAddr;
if ( colorPort )
BaseAddr = (*(PixMapHandle)BaseAddr)->baseAddr;
onScreen = ( BaseAddr == LMGetScrnBase() );
if ( !onScreen ||
( saveGW->txSize >= 32 ) ||
( txFont == 0 ) ||
( txFont == 1/*&& txSize == 48*/ ) ||
( txFont == 3 ) ||
( ( ( pixelSize < 8 ) &&
( ( pixelSize != 4 ) || TestDeviceAttribute ( saveGD, gdDevType ) ) ) ) ) {
#ifdef __powerc
CallUniversalProc ( oldDrawText, uppDrawTextProcInfo, buffer, offset, len );
#else
( ( DrawTextType ) oldDrawText ) ( buffer, offset, len );
#endif
ExitCodeResource();
return;
}
if ( LMGetFractEnable() == 0 )
SetFractEnable ( true );
GetFontInfo ( &fInfo );
GetPen ( &penPt );
SetGWorld ( largeWorld, 0 );
TextFont ( txFont );
TextSize ( txSize );
if ( LockPixels ( largePixMap = GetGWorldPixMap ( largeWorld ) ) ) {
Point endPt;
EraseRect ( &largeRect );
TextFace ( saveGW->txFace );
if ( txMode == grayishTextOr ) {
TextMode ( grayishTextOr );
txMode = srcOr;
}
else
TextMode ( srcCopy );
MoveTo ( 0, largeRect.top + 4 * ( saveGW->txSize + 1 - fInfo.descent) );
#ifdef __powerc
CallUniversalProc ( oldDrawText, uppDrawTextProcInfo, buffer, offset, len );
#else
( ( DrawTextType ) oldDrawText ) ( buffer, offset, len );
#endif
GetPen ( &endPt );
width = ( endPt.h + 3 ) / 4;
myRect.left = penPt.h;
myRect.bottom = penPt.v + fInfo.descent;
myRect.right = myRect.left + width + 1;
myRect.top = myRect.bottom - saveGW->txSize - 1;
maskRect = myRect;
OffsetRect ( &maskRect, - maskRect.left, - maskRect.top );
{
PixMapHandle smallPixMap, savePixMap;
if ( !LockPixels ( smallPixMap = GetGWorldPixMap ( smallWorld ) ) )
SysBeep ( 10 );
if ( !LockPixels ( savePixMap = GetGWorldPixMap ( saveGW ) ) )
SysBeep ( 10 );
ShrinkMap ( largePixMap, smallPixMap, maskRect.right, maskRect.bottom );
SetGWorld ( saveGW, saveGD );
CopyBits ( (BitMap *)*smallPixMap, (BitMap *)*savePixMap,
&maskRect, &myRect, txMode, 0L );
UnlockPixels ( smallPixMap );
UnlockPixels ( savePixMap);
}
UnlockPixels ( largePixMap );
}
Move ( width, 0 );
ExitCodeResource(); // hack
}
void ShrinkMap ( PixMapHandle srceMap, PixMapHandle destMap, short width, short height )
{
Byte *srceBase, *destBase, *destBase0,
*srceBase0, *srceBase1, *srceBase2, *srceBase3;
short row, byte;
short rowBytes, numBytes;
long *table3 = table2;
if ( width >1024 )
width = 1024;
if ( height > 32 )
height = 32;
rowBytes = (*destMap)->rowBytes & 0x7FFF;
numBytes = ( ( width * 4 + 15 ) >> 4 ) << 1;
srceBase = (Byte *)GetPixBaseAddr ( srceMap );
destBase = (Byte *)GetPixBaseAddr ( destMap );
destBase0 = destBase;
if ( numBytes > rowBytes )
numBytes = rowBytes;
for ( row = 0; row < height; row++ ) {
srceBase3 = rowBytes + ( srceBase2 = rowBytes + ( srceBase1 = rowBytes + ( srceBase0 = srceBase ) ) );
for ( byte = numBytes; byte > 0; byte-- ) {
long result;
result = table3[*(srceBase0++)] + table3[*(srceBase1++)] + table3[*(srceBase2++)] +table3[*(srceBase3++)];
result -= ( result & 0x00001010 ) >> 4;
*(destBase0++) = result + (result >> 4);
}
srceBase += rowBytes << 2;
destBase0 = ( destBase += rowBytes );
}
}
Boolean AntiAlias ( void )
{
CTabHandle ctab = GetCTable ( 4 + 32);
{ long i, j;
for ( i = 0; i < 16; i++ )
for ( j = 0; j < 16; j++ )
table2[(i << 4 ) + j] = ( table[i] << 8 ) + table[j];
}
if ( NewGWorld( &smallWorld, 4, &smallRect, ctab, 0, 0 ) != noErr ) {
DisposHandle( (Handle)ctab );
return false;
}
if ( NewGWorld( &largeWorld, 1, &largeRect, 0, 0, 0 ) != noErr ) {
DisposeGWorld ( smallWorld );
return false;
}
SetFractEnable ( true );
return true;
}